/**
 * Description:
 * Class Name:CrazyItArray
 * Date:2022/5/1 13:58
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第18章_类加载机制与反射._18_6_反射和泛型._18_6_1_泛型和Class类;

import java.lang.reflect.Array;

public class CrazyItArray {
    @SuppressWarnings("unchecked")
    public static <T> T[] newInstance(Class<T> componentType, int length){
        return (T[]) Array.newInstance(componentType, length);
    }

    public static void main(String[] args) {
        // 使用CrazyItArray的newInstance()方法来创建一维数组
        String[] arr = CrazyItArray.newInstance(String.class, 10);
        // 若要创建二维数组，需要将类型设置为一维数组的类型即可
        int[][] intArr = CrazyItArray.newInstance(int[].class, 5);
        arr[5] = "Crazy Java";

        intArr[1] = new int[]{23, 12};
        System.out.println(arr[5]);
        System.out.println(intArr[1][1]);
    }
}
